package com.hy.movie.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.movie.common.CommonResult;

import com.hy.movie.model.bo.OrderBo;
import com.hy.movie.model.dto.OrderDto;
import com.hy.movie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Value("${jwt.header}")
    private String tokenHead;
    @Autowired
    private OrderService orderService;
    @PostMapping("/place")
    public CommonResult place(@RequestBody OrderDto orderDto, HttpServletRequest request){
        String token = request.getHeader(tokenHead).split(" ")[1];
        try {
            OrderBo orderBo = orderService.placeOrder(orderDto, token);
            ObjectMapper objectMapper=new ObjectMapper();
            String json = objectMapper.writeValueAsString(orderBo);
            return new CommonResult(0,"success",json);
        }catch (Exception e){
            return new CommonResult(1,"fail",e.getMessage());
        }

    }
}
